'use strict'
const exec = require('child_process').exec
const co = require('co')
// const prompt = require('co-prompt')
const template = require('./template')
const chalk = require('chalk')
const inquirer = require('inquirer')
const generatePage = require('./generatePage.js')
var fs = require("fs");

module.exports = () => {
  co(function* () {
    inquirer.prompt(generatePage.promptName).then(name => {
      inquirer.prompt(generatePage.promptType).then(type => {
        const { templateName } = name;
        const { templateType } = type;
        const templateInfo = template.init[templateType];

        // console.log(templateName);
        // console.log(templateType);
        // console.log(templateInfo);

        let cmdStr = `git clone ${templateInfo.url} ${templateName} && cd ${templateName} && git checkout ${templateInfo.branch}`

        fs.exists(`${templateName}`, function (exists) {
          console.log(exists);
          
          if (exists) {
            console.log("文件已存在")
            process.exit()
            return
          }

          exec(cmdStr, (error, stdout, stderr) => {
            if (error) {
              console.log(error)
              process.exit()
            }
            console.log(chalk.green('\n √ Generation completed!'))
            console.log(`\n cd ${templateName} && npm install \n`)
            process.exit()
          })

        })

      })
    })
  })
}